

package com.zdkj.subway.stm.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.dto.in.SwStInvoiceCheckDTO;
import com.zdkj.subway.stm.entity.SwStInvoiceCatCheck;
import com.zdkj.subway.stm.entity.SwStInvoiceCatCheck;
import com.zdkj.subway.stm.enums.InvoiceChechAuditTypeEnum;
import com.zdkj.subway.stm.service.SwStInvoiceCatCheckService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 长安通定额发票盘点
 *
 * @author cxp
 * @date 2024-11-20 20:09:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstinvoicecatcheck")
@Api(value = "swstinvoicecatcheck", tags = "长安通定额发票盘点管理")
public class SwStInvoiceCatCheckController {

    private final SwStInvoiceCatCheckService swStInvoiceCatCheckService;

    /**
     * 分页查询
     *
     * @param page                分页对象
     * @param swStInvoiceCatCheck 长安通定额发票盘点
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwStInvoiceCatCheckPage(Page page, SwStInvoiceCatCheck swStInvoiceCatCheck) {		
        return R.ok(swStInvoiceCatCheckService.page(page, swStInvoiceCatCheck));
    }


    /**
     * 通过id查询长安通定额发票盘点
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(swStInvoiceCatCheckService.getById(id));
    }

    /**
     * 新增长安通定额发票盘点
     *
     * @param swStInvoiceCatCheck 长安通定额发票盘点
     * @return R
     */
    @ApiOperation(value = "新增长安通定额发票盘点", notes = "新增长安通定额发票盘点")
    @SysLog("新增长安通定额发票盘点")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_add')")
    public R save(@RequestBody SwStInvoiceCatCheck swStInvoiceCatCheck) {
        return R.ok(swStInvoiceCatCheckService.save(swStInvoiceCatCheck));
    }

    /**
     * 修改长安通定额发票盘点
     *
     * @param swStInvoiceCatCheck 长安通定额发票盘点
     * @return R
     */
    @ApiOperation(value = "修改长安通定额发票盘点", notes = "修改长安通定额发票盘点")
    @SysLog("修改长安通定额发票盘点")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_edit')")
    public R updateById(@RequestBody SwStInvoiceCatCheck swStInvoiceCatCheck) {
        return R.ok(swStInvoiceCatCheckService.updateById(swStInvoiceCatCheck));
    }

    /**
     * 通过id删除长安通定额发票盘点
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除长安通定额发票盘点", notes = "通过id删除长安通定额发票盘点")
    @SysLog("通过id删除长安通定额发票盘点")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(swStInvoiceCatCheckService.removeById(id));
    }

    /**
     * 通过id批量删除长安通定额发票盘点
     * @param idList
     * @return
     */
    @ApiOperation(value = "通过id批量删除长安通定额发票盘点", notes = "通过id批量删除长安通定额发票盘点")
    @SysLog("通过id批量删除长安通定额发票盘点")
    @PostMapping("/batchDelete")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_batch_del')")
    public R batchRemoveByIds(@RequestBody List<Integer> idList) {
        idList.forEach(swStInvoiceCatCheckService::removeById);
        return R.ok();
    }

    /**
     * 通过日期查询前一月盘点数据
     *
     * @param checkTime
     * @return R
     */
    @ApiOperation(value = "通过日期查询前一月盘点数据", notes = "通过日期查询前一月盘点数据")
    @GetMapping("/getPreviousMonthInvoiceCatCheck")
    public R getPreviousMonthInvoiceCatCheck(String checkTime) {
        return R.ok(swStInvoiceCatCheckService.getPreviousMonthInvoiceCatCheck(checkTime, 0));
    }

    /**
     * 通过日期查询当月长安通定额发票盘点数据
     *
     * @param checkTime
     * @return R
     */
    @ApiOperation(value = "通过日期查询当月备用金盘点数据", notes = "通过日期查询当月备用金盘点数据")
    @GetMapping("/getInvoiceCatCheck")
    public R getTicketCheck(String checkTime) {
        PigxUser user = SecurityUtils.getUser();
        LambdaQueryWrapper<SwStInvoiceCatCheck> queryWrapper = Wrappers.<SwStInvoiceCatCheck>lambdaQuery()
                .eq(SwStInvoiceCatCheck::getLineCode, user.getLineCode())
                .eq(SwStInvoiceCatCheck::getStationCode, user.getStationCode())
                .eq(SwStInvoiceCatCheck::getDelFlag, "0")
                .eq(SwStInvoiceCatCheck::getStatisticsDate, checkTime)
                .notIn(SwStInvoiceCatCheck::getCheckStatus,2,4,6);
        return R.ok(swStInvoiceCatCheckService.getMap(queryWrapper));
    }

    @ApiOperation(value = "通过日期查询当月备用金盘点数据", notes = "通过日期查询当月备用金盘点数据")
    @GetMapping("/getInvoiceCatCheckEdit")
    public R getTicketCheck(Integer id,String checkTime) {
        PigxUser user = SecurityUtils.getUser();
        LambdaQueryWrapper<SwStInvoiceCatCheck> queryWrapper = Wrappers.<SwStInvoiceCatCheck>lambdaQuery()
                .eq(SwStInvoiceCatCheck::getLineCode, user.getLineCode())
                .eq(SwStInvoiceCatCheck::getStationCode, user.getStationCode())
                .eq(SwStInvoiceCatCheck::getDelFlag, "0")
                .eq(SwStInvoiceCatCheck::getStatisticsDate, checkTime)
//                .notIn(SwStInvoiceCatCheck::getCheckStatus,2,4,6)
                .ne(SwStInvoiceCatCheck::getId, id);
        return R.ok(swStInvoiceCatCheckService.getMap(queryWrapper));
    }

    /**
     * 新增或修改长安通定额发票盘点
     *
     * @param swStInvoiceCatCheck 长安通定额发票盘点
     * @return R
     */
    @ApiOperation(value = "新增或修改长安通定额发票盘点", notes = "新增或修改长安通定额发票盘点")
    @SysLog("新增或修改长安通定额发票盘点")
    @PostMapping("saveOrUpdate")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_add_or_update')")
    public R saveOrUpdate(@RequestBody SwStInvoiceCatCheck swStInvoiceCatCheck) {
        return R.ok(swStInvoiceCatCheckService.saveOrUpdateInvoiceCatCheck(swStInvoiceCatCheck));
    }

    /**
     * 长安通定额发票盘点数据批量提交
     */
    @ApiOperation(value = "长安通定额发票盘点数据批量提交", notes = "长安通定额发票盘点数据批量提交")
    @PutMapping("/modify/submit/status")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_batch_submit')")
    public R updateSubmitStatus(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStInvoiceCatCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 长安通定额发票盘点数据批量审核
     */
    @ApiOperation(value = "长安通定额发票盘点数据批量审核", notes = "长安通定额发票盘点数据批量审核")
    @PutMapping("/modify/approval/status")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_batch_approval')")
    public R updateApprovalStatus(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStInvoiceCatCheckService.updateApprovalStatus(ids, status, InvoiceChechAuditTypeEnum.AUDIT_STATION.getCode()));
    }

    /**
     * 长安通定额发票盘点数据批量驳回
     */
    @ApiOperation(value = "长安通定额发票盘点数据批量驳回", notes = "长安通定额发票盘点数据批量驳回")
    @PutMapping("/modify/reject/status")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_batch_reject')")
    public R updateRejectStatus(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStInvoiceCatCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 长安通定额发票盘点数据车间票务专业审核
     */
    @ApiOperation(value = "长安通定额发票盘点数据车间票务专业审核", notes = "长安通定额发票盘点数据车间票务专业审核")
    @PutMapping("/modify/workshopApproval/status")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_workshop_approval')")
    public R updateWorkshopApproval(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStInvoiceCatCheckService.updateApprovalStatus(ids, status, InvoiceChechAuditTypeEnum.AUDIT_WORKSHOP.getCode()));
    }

    /**
     * 长安通定额发票盘点数据车间票务专业驳回
     */
    @ApiOperation(value = "长安通定额发票盘点数据车间票务专业驳回", notes = "长安通定额发票盘点数据车间票务专业驳回")
    @PutMapping("/modify/workshopReject/status")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_workshop_reject')")
    public R updateWorkshopReject(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStInvoiceCatCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 长安通定额发票盘点数据部门票务专业审核
     */
    @ApiOperation(value = "长安通定额发票盘点数据部门票务专业审核", notes = "长安通定额发票盘点数据部门票务专业审核")
    @PutMapping("/modify/departmentApproval/status")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_department_approval')")
    public R updateDepartmentApproval(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStInvoiceCatCheckService.updateApprovalStatus(ids, status, InvoiceChechAuditTypeEnum.AUDIT_DEPT.getCode()));
    }

    /**
     * 长安通定额发票盘点数据部门票务专业驳回
     */
    @ApiOperation(value = "长安通定额发票盘点数据部门票务专业驳回", notes = "长安通定额发票盘点数据部门票务专业驳回")
    @PutMapping("/modify/departmentReject/status")
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicecatcheck_department_reject')")
    public R updateDepartmentReject(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStInvoiceCatCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 车站导出长安通定额发票盘点
     * @param response
     */
    @PostMapping("/exportInvoiceCatCheckForStation")
    @SysLog("车站导出长安通定额发票盘点")
    public void exportInvoiceCatCheckForStation(@RequestBody Long id, HttpServletResponse response) {
        swStInvoiceCatCheckService.exportInvoiceCatCheckForStation(id, response);
    }

	 /**
     * 车站导出长安通定额发票盘点
     * @param response
     */
    @PostMapping("/exportInvoiceCatCheckForStationAll")
    //@SysLog("车站导出长安通定额发票盘点")
    public void exportInvoiceCatCheckForStationAll(@RequestBody String  checkTime, HttpServletResponse response) {
        swStInvoiceCatCheckService.exportInvoiceCatCheckForStationAll(checkTime, response);
    }

    /**
     * 审核页面导出长安通定额发票和西安中铁定额发票盘点
     * @param response
     */
    @PostMapping("/exportInvoiceCheckForApproval")
    @SysLog("审核页面导出长安通定额发票和西安中铁定额发票盘点")
    public void exportInvoiceCatCheckForApproval(@RequestBody SwStInvoiceCheckDTO swStInvoiceCheckDTO, HttpServletResponse response) {
        swStInvoiceCatCheckService.exportInvoiceCatCheckForApproval(swStInvoiceCheckDTO, response);
    }

}
